<template>
  <div class="container my-info">
    <!-- <van-nav-bar
      title="修改密码"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    /> -->
    <div class="input-group">
      <!-- <van-cell-group>
        <van-field
          v-model="phone"
          label="手机号"
          readonly
        />
      </van-cell-group> -->
      <van-cell-group>
        <van-field
          v-model="oldPassword"
          label="原密码"
          type="password"
          placeholder="请输入原密码"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="newPassword"
          label="新密码"
          type="password"
          placeholder="请输入新密码"
        />
      </van-cell-group>
    </div>
    <div class="my-info-btn-row">
        <van-button type="danger" class="save-btn" @click="updateUserInfo">修改</van-button>
    </div>
  </div>
</template>

<script>
//import { msToDate } from '@/until/until';
export default {
  name: 'updatePassword',
  data() {
    return {
      oldPassword:'',
      phone:'',
      newPassword:'',
    };
  },
  mounted() {
    // this.getUserInfo();
    let userInfo = JSON.parse(localStorage.getItem("userInfo"));
    this.phone = userInfo.Phone;
    if(!this.phone){
      setTimeout(()=>{
        this.$dialog.confirm({
          title: '提示',
          message: '尚未绑定手机号，请先前往绑定手机号，才能修改密码。是否前往？'
        }).then(() => {
          // on confirm
          this.$router.push({
            path: '/myInfo'
          })
        }).catch(() => {
          // on cancel
        });
      },200)
    }
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    updateUserInfo(){
      if(this.oldPassword == ''){
        this.$toast('请输入原密码')
        return false;
      }else{
        if(this.oldPassword.length < 6){
          this.$toast('原密码长度至少为6位')
          return false;
        }
      }
      if(this.newPassword == ''){
        this.$toast('请输入新密码')
        return false;
      }else{
        if(this.newPassword.length < 6){
          this.$toast('新密码长度至少为6位')
          return false;
        }
      }
      if(this.oldPassword == this.newPassword){
        this.$toast('新密码不能与原密码一致')
          return false;
      }
      let params = {
        OldPwd:this.oldPassword,
        NewPwd:this.newPassword
      }
      let userType = localStorage.getItem("userType");
      if(userType=='0'){
        this.$api.mine.modifyUserPassword(params).then(res=> {
          if(res.IsSuccess){
            this.$toast.success('修改成功,请重新登录');  
            setTimeout(()=>{
                localStorage.removeItem("authorization");
                this.$router.push({
                  path: '/login',
                  query:{ userType:userType }
              })
            },2000) 
          }
        })    
      }else{
        this.$api.mine.updateShopPassword(params).then(res=> {
          if(res.IsSuccess){
            this.$toast.success('修改成功,请重新登录');  
            setTimeout(()=>{
                localStorage.removeItem("authorization");
                this.$router.push({
                  path: '/login',
                  query:{ userType:userType }
              })
            },2000) 
          }
        })    
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.my-info {
  .input-group {
    margin-top: .266667rem;
  }
  .select-time{
    position: absolute;
    bottom: 0;
    width: 100%;
  }
  .my-info-btn-row {
    margin-top: 1.066667rem;
    text-align: center;

    .save-btn {
      width: 90%;
    }
  }
}
</style>